package top.glkj.hms.utils;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.AuthenticationException;
import top.glkj.hms.exception.DefineException;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 验证码类
 *
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/8/24 18:20
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class VerificationCode {
    public Long id = 1L;
    private String code = "";
    private BufferedImage bufferedImage;

    public static void checkCode(String code, String record) throws AuthenticationException {
        if (record == null || record.trim().length() < StaticValue.SIX) {
            throw new CodeAuthenticationException("验证图片失效,请刷新验证码后再试");
        }
        if (code == null) {
            throw new CodeAuthenticationException("验证码输入为空");
        } else if (!code.toUpperCase().trim().equals(record.toUpperCase().trim())) {
            throw new CodeAuthenticationException("验证码输入错误");
        }
    }

    /**
     * 生成一个由0-9组成的 size 位数的验证码
     *
     * @param size 验证码长度，4-10之间
     * @return String 验证码
     */
    public static String getDefineIntCode(int size) throws DefineException {
        if (size < StaticValue.FOUR || size > StaticValue.TEN) {
            throw new DefineException("验证码生成失败，验证码长度在4-10之间");
        }
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < size; i++) {
            sb.append(random.nextInt(StaticValue.TEN));
        }
        return sb.toString();
    }

    public static void defineIntCode(String code1, String code2) throws DefineException {
        try {
            int codeInt1 = Integer.parseInt(code1);
            int codeInt2 = Integer.parseInt(code2);
            if (codeInt1 != codeInt2) {
                throw new DefineException("验证码输入错误");
            }
        } catch (Exception e) {
            throw new DefineException("验证码输入错误，请输入字符串!");
        }
    }

    public BufferedImage getImageCode() {
        int charNum = StaticValue.SIX;
        int width = 20 * 6;
        int height = 30;
        //1.创建一张内存图片
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        //2.获得绘图对象
        Graphics graphics = bufferedImage.getGraphics();

        //3.绘制背景颜色
        graphics.setColor(Color.WHITE);
        graphics.fill3DRect(0, 0, width, height, true);
        //4.绘制图片边框
        graphics.setColor(Color.GRAY);
        graphics.drawRect(0, 0, width - 2, height - 2);

        //5.输出验证码内容
        graphics.setColor(Color.RED);
        graphics.setFont(new Font("宋体", Font.BOLD, 20));

        //6.随机输出6个字符
        String s = "ABCDEFGHIJKLMNOPQRSTabcdefghjkmnopqrst23456789";
        Random random = new Random();


        int x = 5;
        for (int i = 0; i < charNum; i++) {
            int index = random.nextInt(s.length());
            String content = String.valueOf(s.charAt(index));

            code += content;
            graphics.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
            graphics.drawString(content, x, 22);
            x += 20;
        }

        // 6、绘制干扰线
        graphics.setColor(Color.GRAY);
        for (int i = 0; i < StaticValue.FIVE; i++) {
            int x1 = random.nextInt(width);
            int x2 = random.nextInt(width);

            int y1 = random.nextInt(height);
            int y2 = random.nextInt(height);
            graphics.drawLine(x1, y1, x2, y2);
        }
        graphics.dispose();
        return bufferedImage;
    }

    public static class CodeAuthenticationException extends AuthenticationException {

        public CodeAuthenticationException(String msg) {
            super(msg);
        }
    }
}
